package com.itheima.service.impl.ums;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.core.PageInfo;
import com.itheima.mapper.ums.UmsMenuMapper;
import com.itheima.pojo.ums.UmsMenu;
import com.itheima.pojo.ums.vo.UmsMenuVo;
import com.itheima.service.ums.UmsMenuService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@DubboService
public class UmsMenuServiceImpl extends ServiceImpl<UmsMenuMapper, UmsMenu> implements UmsMenuService {

    /**
     * 分页查询后台菜单
     * @param parentId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo selectList(Integer parentId, Integer pageNum, Integer pageSize) {
        Page<UmsMenu> listPageInfo = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<UmsMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UmsMenu::getParentId,parentId).orderByDesc(UmsMenu::getSort);
        Page<UmsMenu> page = this.page(listPageInfo,queryWrapper);
        int total = (int) page.getTotal();
        int current = (int) page.getCurrent();
        //封装分页数据
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageNum(pageNum);
        pageInfo.setPageSize(pageSize);
        pageInfo.setTotal(total);
        pageInfo.setTotalPage(current);
        pageInfo.setList(page.getRecords());
        return pageInfo;
    }

    /**
     * 修改后台菜单
     * @param umsMenu
     * @return
     */
    @Override
    public void updateMenu(UmsMenu umsMenu) {
        this.updateById(umsMenu);
    }

    /**
     * 修改菜单显示状态
     * @param id
     * @param hidden
     */
    @Override
    public void updateHidden(Integer id, Integer hidden) {
        UmsMenu umsMenu = this.getById(id);
        umsMenu.setHidden(hidden);
        this.save(umsMenu);
    }


    /**
     * 根据Id获取菜单详情
     * @param id
     * @return
     */
    @Override
    public UmsMenu selectMenu(Integer id) {
        UmsMenu umsMenu = this.getById(id);
        return umsMenu;
    }

    /**
     * 树形结构返回所有菜单列表
     *
     */
    @Override
    public  List<UmsMenuVo> treeList() {
        //查询所有的菜单列表
        List<UmsMenu> umsMenuList = this.list();
        List<UmsMenuVo> umsMenuVoList = umsMenuList.stream().map(umsMenu -> {
            UmsMenuVo umsMenuVo=new UmsMenuVo();
          BeanUtils.copyProperties(umsMenu,umsMenuVo);
            return umsMenuVo;
        }).collect(Collectors.toList());

        //查询所有一级菜单
        List<UmsMenuVo> umsMenuVos = umsMenuVoList.stream().filter(item -> "0".equals(String.valueOf(item.getParentId())))
                .map(item -> {
                    item.setChildren(getChildren(item.getId(), umsMenuVoList));
                    return item;
                }).sorted(Comparator.comparing(UmsMenuVo::getSort))
                .collect(Collectors.toList());
        return  umsMenuVos;
    }

    //递归查找子分类
    private List<UmsMenuVo> getChildren(Long id,List<UmsMenuVo> all){
        List<UmsMenuVo> umsMenuVoList = all.stream().filter(umsMenuVo -> umsMenuVo.getParentId().equals(id))
                .map(umsMenuVo -> {
                    umsMenuVo.setChildren(getChildren(umsMenuVo.getId(), all));
                    return umsMenuVo;
                }).sorted(Comparator.comparing(UmsMenu::getSort))
                .collect(Collectors.toList());

        return umsMenuVoList;
    }

}
